package com.zh.webcommon.common.param;

import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;


/**
 * 基本的请求条件参数
 * 建议：查询条件的string设置默认值为"";
 */
public class CommonParam implements Serializable {
    public static final int QUERY_MAX_SIZE = 10000;
    public static final int BATCH_MAX_SIZE = 1000;
    /**
     * 返回记录排序方向
     */
    static final String SORT_ASC = "ASC";
    /**
     * 返回记录排序方向
     */
    static final String SORT_DESC = "DESC";

    /**
     * 查询时间段
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    protected LocalDateTime startTime;
    @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    protected LocalDateTime endTime;

    /**
     * 排序
     */
    protected boolean asc = true; //默认排序方向ASC
    protected String sortField;     //默认排序字段

    /**
     * 分页
     */
    protected int num = 1; //默认当前页号
    protected int size = QUERY_MAX_SIZE; //默认页大小

    private long recordTotal = 0;   //查询到的总记录数

    public CommonParam() {
    }

    public CommonParam(int pageNum, int pageSize) {
        this.num = pageNum;
        this.size = pageSize;
    }

    public CommonParam sort(String field, boolean isAsc) {
        Objects.requireNonNull(field, "排序字段值非法");
        this.sortField = field;
        this.asc = isAsc;
        return this;
    }

    public String getSortField() {
        return sortField;
    }

    public void setSortField(String sortField) {
        this.sortField = sortField;
    }

    public boolean isAsc() {
        return asc;
    }

    public void setAsc(boolean asc) {
        this.asc = asc;
    }

    public String sortName() {
        return isAsc() ? SORT_ASC : SORT_DESC;
    }

    public LocalDateTime getStartTime() {
        return startTime;
    }

    public CommonParam setStartTime(LocalDateTime startTime) {
        this.startTime = startTime;
        return this;
    }

    public LocalDateTime getEndTime() {
        return endTime;
    }

    public CommonParam setEndTime(LocalDateTime endTime) {
        this.endTime = endTime;
        return this;
    }

    public Integer getNum() {
        return num;
    }

    public CommonParam setNum(Integer num) {
        this.num = num;
        return this;
    }

    public Integer getSize() {
        return size;
    }

    public CommonParam setSize(Integer size) {
        this.size = size;
        return this;
    }

    public long getRecordTotal() {
        return recordTotal;
    }

    public CommonParam setRecordTotal(long recordTotal) {
        this.recordTotal = recordTotal;
        return this;
    }

    public int offset() {
        return this.num * this.size;
    }

    /**
     * 字符普通化，即清除清除特殊字符，去除首尾空格
     * 提供这个方法的初衷是用于数据库like匹配时，清除特殊字符
     *
     * @param str 可以为null
     * @return 字符串，不为null
     */
    public static String stringPretty(String str) {
        if (str != null) {
            return str.trim().replaceAll("[~!#$%^&*()?'\"]+", "");
        } else {
            return "";
        }
    }
}
